package com.github.bryx.workflowweb.controller.manager;

import com.github.bryx.workflow.domain.WorkflowDef;
import com.github.bryx.workflow.dto.buildtime.*;
import com.github.bryx.workflow.service.WorkflowBuildTimeManager;
import com.github.bryx.workflowweb.config.entity.ResultEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author lancer.zyj
 * @date 2021/7/7 7:14 下午
 */
@RestController
@RequestMapping("/build/manage")
@Api(tags = "工作流管理")
public class WorkflowBuildTimeManagerController {

    @Autowired
    private WorkflowBuildTimeManager workflowBuildTimeManager;

    @PostMapping("/create/draft")
    @ApiOperation(value = "创建流程定义草稿",notes = "如果没有指定版本，则创建流程定义及版本")
    public ResultEntity<WorkflowDef> createWorkflowDefDraft(@RequestBody @Valid CreateWorkflowDefDraftDto param){
        return ResultEntity.of(workflowBuildTimeManager.createWorkflowDefDraft(param));
    }

    @PostMapping("/update/draft")
    @ApiOperation(value = "修改流程定义版本草稿")
    public ResultEntity<String> updateWorkflowDefDraft(@RequestBody @Valid UpdateWorkflowDefDraftDto param){
        workflowBuildTimeManager.updateWorkflowDefDraft(param);
        return ResultEntity.ofSuccess();
    }

    @PostMapping("/update/rev")
    @ApiOperation(value = "修改流程定义版本草稿")
    public ResultEntity<String> updateWorkflowDefRev(@RequestBody @Valid UpdateWorkflowDefRevDto param){
        workflowBuildTimeManager.updateWorkflowDefRev(param);
        return ResultEntity.ofSuccess();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改流程定义的基础信息")
    public ResultEntity<String> updateWorkflowDef(@RequestBody @Valid UpdateWorkflowDefDto param){
        workflowBuildTimeManager.updateWorkflowDef(param);
        return ResultEntity.ofSuccess();
    }

    @PostMapping("/publish/draft")
    @ApiOperation(value = "通过草稿发布流程定义到最新版本")
    public ResultEntity<String> publishWorkflowDefDraft(@RequestBody @Valid PublishWorkflowDefDraftDto param){
        workflowBuildTimeManager.publishWorkflowDefDraft(param);
        return ResultEntity.ofSuccess();
    }

    @PostMapping("/status")
    @ApiOperation(value = "修改流程定义版本状态")
    public ResultEntity<String> changeWorkflowDefRevStatus(@RequestBody @Valid ChangeWorkflowDefRevStatusDto param){
        workflowBuildTimeManager.changeWorkflowDefRevStatus(param);
        return ResultEntity.ofSuccess();
    }

    @PostMapping("/del/{workflowDefId}")
    @ApiOperation(value = "删除流程定义")
    public ResultEntity<String> deleteWorkflowDef(@PathVariable String workflowDefId){
        workflowBuildTimeManager.deleteWorkflowDef(workflowDefId);
        return ResultEntity.ofSuccess();
    }

    @PostMapping("/del/force/{workflowDefId}")
    @ApiOperation(value = "彻底删除流程定义")
    public ResultEntity<String> forceDeleteWorkflowDef(@PathVariable String workflowDefId){
        workflowBuildTimeManager.forceDeleteWorkflowDef(workflowDefId);
        return ResultEntity.ofSuccess();
    }

    @PostMapping("/del/rev/{workflowDefId}")
    @ApiOperation(value = "删除流程定义版本")
    public ResultEntity<String> deleteWorkflowDefRev(@PathVariable String workflowDefId){
        workflowBuildTimeManager.deleteWorkflowDefRev(workflowDefId);
        return ResultEntity.ofSuccess();
    }

}
